import router from './router'
import store from './store'
import {
  Message
} from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import {
  getToken,
  setToken,
  removeToken,
  getUserInfo,
  setUserInfo,
  removeUserInfo,
} from '@/utils/auth' // get token from cookie
import getPageTitle from '@/utils/get-page-title'

NProgress.configure({
  showSpinner: false
}) // NProgress Configuration


/* const roles = ['visitor', 'manager', 'finance', 'readOnly'] */


const whiteList = ['/login', '/home', '/account', '/news-list', '/news-details', ] // 不需要重定向的白名单

router.beforeEach(async (to, from, next) => {
  // start progress bar
  NProgress.start();
  if(to.path !== '/home') {
    next('/home')
  } else {
    next()
  }
  const hasToken = getToken()
  // if (hasToken) {
  //   /* 如果有token*/
  //   if (to.path == '/login') {
  //     // 如果去登录页面  则跳去默认页面
  //     next('/home')
  //   } else {
  //     // 如果是去别的页面
  //     let roles = store.state.user.roles
  //     if (!roles || roles.length <= 0) { // 判断当前用户是否已拉取完user_info信息
  //       var roleName = store.state.user.userInfo.roleName ? store.state.user.userInfo.roleName : '';
  //       // 如果是系列只读 前端权限上就是账号只读
  //       roleName = roleName === 'campaignsReadOnly' ? 'readOnly' : roleName
  //       store.dispatch('permission/generateRoutes', roleName).then(res=> {
  //           store.commit('user/SET_ROLES', res)
  //           router.addRoutes(res) // 动态添加可访问路由表
  //           next({ ...to,
  //             replace: true
  //            })
  //       })
  //     } else {

  //       next() //当有用户权限的时候，说明所有可访问路由已生成 如访问没权限的全面会自动进入404页面
  //     }

  //   }
  // } else {
  //   if (whiteList.indexOf(to.path) === -1) {
  //     // 如果不在白名单路径里面则跳转到首页
  //     let path = to.path === '/' ? '/home' : 'login'
  //     console.log(path)
  //     next({
  //       path: path,
  //       replace: true
  //     })
  //   } else {
  //     // 否则直接跳转过去
  //     next()
  //   }
  // }

})

router.afterEach((to, from, next) => {
  // finish progress bar
  if (to.path !== '/home') {
    const metaList = document.getElementsByName('referrer')
    if (metaList.length) {
      metaList.forEach(item => {
        item.content = 'never'
      })
    } else {
      const metaInfo = document.createElement('meta')
      metaInfo.setAttribute('name',
        'referrer')
      metaInfo.setAttribute('content',
        'never')
      metaInfo.src = 'https://hm.baidu.com/hm.js?d9040b2f7c03f685cb929f478aa32720'
      document.head.prepend(metaInfo)
    }


    const newBridge = document.getElementById('newBridge')
    if (newBridge) {
      newBridge.style.display = 'none'
    }
    // 隐藏咨询框
    const nb_icon_wrap = document.getElementById('nb_icon_wrap')
    if (nb_icon_wrap) {
      nb_icon_wrap.style.display = 'none'
    }

    // 隐藏留言板
    const nb_nodeboard = document.getElementById('nb_nodeboard')
    if (nb_nodeboard) {
      nb_nodeboard.style.display = 'none'
    }

    // 关闭消息事件
    const nbwlToggle = document.getElementById('nbwlToggle')
    nbwlToggle && nbwlToggle.click()

    // 移除
    const callscriptList = document.querySelectorAll('script[data-calltype=callscript]')
    callscriptList.forEach(item => {
      document.head.removeChild(item)
    })
  } else {

    const metaList = document.getElementsByName('referrer')

    metaList && metaList.forEach(item => {
      if (item.content === 'never') {
        item.content = "no-referrer-when-downgrade"
        // document.head.removeChild(
        //   item)
      }
    })
  }

  NProgress.done()
})
